﻿using Dict.DataModel;
using System.Collections;
using UnityEngine;
namespace Story
{
    class StepMoveCamera : StepBase
    {
        private static Transform oriTarget = null;
        private static Transform lastTarget = null;
        protected override void OnStart()
        {
            //TODO
            float time = 0f;
            float.TryParse(actionParam1, out time);
            Vector3 targetPos = GetPosition(actionParam2);

            var tmpTarget = new GameObject("TmpCameraFollowObject").transform;

            if(oriTarget==null)
            {
                oriTarget = Room.RoomManager.currentRoom.sceneCamera.target;
            }
            tmpTarget.position = Room.RoomManager.currentRoom.sceneCamera.target.position;

            if (lastTarget != null)
            {
                GameObject.Destroy(lastTarget.gameObject);
                lastTarget = null;
            }
            lastTarget = tmpTarget;
            Room.RoomManager.currentRoom.sceneCamera.SetTarget(lastTarget);

            TweenActionMove a1 = new TweenActionMove(lastTarget, targetPos, time, true);
            TweenAction tweenComponent = storyManager.gameObject.AddMissingComponent<TweenAction>();
            tweenComponent.RunAction(a1);

            if(actionParam2.Equals("selfPos"))
            {
                storyManager.StartCoroutine(OnMoveEnd(time));
            }
            End();
        }

        private IEnumerator OnMoveEnd(float time)
        {
            yield return new WaitForSeconds(time);
            if(oriTarget!=null)
            {
                Room.RoomManager.currentRoom.sceneCamera.SetTarget(oriTarget);
                oriTarget = null;
            }
        }
    }
}